﻿/* UrlHelper
 */

//待完善：单元测试
namespace LSLib.Net
{
    using System;
    using System.Net;
    using System.Text;

    public static class UrlHelper
    {
        /// <summary>获取拼接参数后的Url
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="args">参数</param>
        public static string GetUrl(string address, params string[] args)
        {
            if (args == null || args.Length == 0) return address;

            StringBuilder result = new StringBuilder();
            foreach (string next in args)
            {
                result.Append("/");
                result.Append(next);
            }
            return string.Concat(address, result.ToString());
        }

        /// <summary>检查url是否有效
        /// </summary>
        public static bool CheckOutUrl(string url)
        {
            if (InternetHelper.IsConnectInternet() == false) throw new Exception("当前网络环境,无法连接到互联网！");

            HttpWebRequest webRequest = null;//HTTP请求
            HttpWebResponse webResponse = null;//HTTP会话

            try
            {
                webRequest = (HttpWebRequest)WebRequest.Create(url);//创建HTTP请求
                webResponse = (HttpWebResponse)webRequest.GetResponse();//获得HTTP会话

                if (webResponse.StatusCode == HttpStatusCode.OK) return true;
            }
            catch { }
            finally
            {
                if (webResponse != null) webResponse.Close();
            }
            return false;
        }

    }
}
